import { defineConfig, loadEnv } from 'vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import eslintPlugin from 'vite-plugin-eslint' //导入包

import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
const path = require('path')
export default defineConfig(({ command, mode }) => {
  loadEnv(mode, process.cwd(), '')
  // const env = loadEnv(mode, process.cwd(), '')
  // console.log('env===', env)
  return {
    base: '/activity/mango_mobile/',
    // publicDir: './',
    plugins: [
      vue(),
      Components({
        resolvers: [VantResolver()]
      }),
      // 增加下面的配置项,这样在运行时就能检查eslint规范
      eslintPlugin({
        include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
      })
    ],
    build: {
      // outDir: env.VITE_APP_NAME,
      outDir: 'mango_mobile',
      // 启动 / 禁用 CSS 代码拆分
      cssCodeSplit: true,
      // 构建后是否生成 soutrce map 文件
      sourcemap: false,
      chunkSizeWarningLimit: 1024,
      rollupOptions: {
        input: {
          main: resolve(__dirname, 'index.html'),
          demo: resolve(__dirname, 'demo.html')
        }
        // output: {
        //   chunkFileNames: 'static/js/[name]-[hash].js',
        //   entryFileNames: 'static/js/[name]-[hash].js',
        //   assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        // }
      }
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src')
      }
    },
    css: {
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import "./src/assets/style/global.less";'
        }
      }
    },
    // rollupOptions: {
    //   output: {
    //     manualChunks(id) {
    //       console.log(`id=${id}`)
    //       if (id.include('node_modules')) {
    //         return id
    //           .toString()
    //           .split('node_module')[1]
    //           .split('/')[0]
    //           .toString()
    //       }
    //     }
    //   }
    // },
    // chunkFileNames: (chunkInfo) => {
    //   const facadeModuleId = chunkInfo.facadeModuleId
    //     ? chunkInfo.facadeModuleId.split('/')
    //     : []
    //   const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'
    //   return `js/${fileName}/[name].[hash].js`
    // },
    server: {
      // post: 3001,
      host: '0.0.0.0',
      open: true
    }
  }
})
